/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.bayu.graph;

import com.bayu.common.lib.CommonUtil;
import com.bayu.controller.MainController;
import com.bayu.services.dao.K3Dao;
import com.bayu.services.entity.K3;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 *
 * @author Bayu
 */
@Controller
public class K3Graph {
    K3Dao k3Dao;

    @Autowired
    private void setk3Dao(K3Dao k3Dao){
      this.k3Dao = k3Dao;
    }
    
    @RequestMapping("/view-k3-graph.htm")
    public void getK3Graph(ModelMap map,HttpServletRequest req){
        String start = req.getParameter("start");
        String end = req.getParameter("end");

        String[] status = {"#FFFFFF","#FFFFFF","#FFFFFF","#FFFFFF","#FFFFFF","#FFFFFF",
        "#FFFFFF","#FFFFFF","#FFFFFF","#FFFFFF","#FFFFFF","#FFFFFF","#FFFFFF","#FFFFFF",
        "#FFFFFF","#FFFFFF","#FFFFFF","#FFFFFF","#FFFFFF","#FFFFFF","#FFFFFF","#FFFFFF",
        "#FFFFFF","#FFFFFF","#FFFFFF","#FFFFFF","#FFFFFF","#FFFFFF","#FFFFFF","#FFFFFF","#FFFFFF"};

        String[] desc = new String[31];
        if(start == null){
            start = CommonUtil.getCurrentYear()+"-"+CommonUtil.getCurrentMonth()+"-01";

        }

        if(end == null ){
            if((CommonUtil.getCurrentMonth()%2)==0){
                if(CommonUtil.getCurrentMonth()==2){
                    end = CommonUtil.getCurrentYear()+"-"+CommonUtil.getCurrentMonth()+"-28";
                }else{
                    end = CommonUtil.getCurrentYear()+"-"+CommonUtil.getCurrentMonth()+"-30";
                }

            }else{
                end = CommonUtil.getCurrentYear()+"-"+CommonUtil.getCurrentMonth()+"-31";
            }


        }
        List data = this.k3Dao.getAllByDate(start, end);
        
        String[] tgl;
        int index;
        for(int i=0;i<data.size();i++){

            K3 k3 = (K3) data.get(i);
            tgl = k3.getDate().toString().split("-");

            index = Integer.valueOf(tgl[2]).intValue()-1;

            if(k3.getNote().equals("T")){
                status[index]="#009000";
            }
            if(k3.getNote().equals("R")){
                status[index]="#FFFF00";
            }
            if(k3.getNote().equals("B")){
                status[index]="#FF0000";
            }

            desc[i]=k3.getDescription();
            
        }

        map.addAttribute("desc", desc);
        map.addAttribute("stat", status);
        map.addAttribute("start", start);
        map.addAttribute("end", end);



    }


}
